Skip to content

Guard comment writes against repeats, and audit orphan agent labels (report-only) - #3395

Merged
Birfy merged 20 commits into
mainfrom
dev/3393-write-guards
Nov 27, 2026
Merged

Birfy merged 20 commits into
mainfrom
dev/3393-write-guards

Conversation

@Birfy

@Birfy Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Related #3393

Two report-only guards for the write path we just watched fail: the same comment landed twice on 19 objects, and agent-working / agent-pr labels stay on objects whose work or PR is gone. Neither script writes anything; wiring them into workflows is a later slice.

What is here

.github/scripts/comment-text.mjs — the text side. <!-- flybot:<id> --> markers, bodyKey fingerprint, Jaccard over 3-grams.
.github/scripts/comment-guard.mjs — guard(comments, event, draft) returns skip / review / post; --check-only exits 0 = post, 3 = already said, 4 = needs a human.
.github/scripts/label-hygiene.mjs — agent-working / agent-pr orphans only; every other label is listed under Not touched and never judged.
Two test files beside them.

Two decisions worth reviewing:

  • Duplicate detection stays inside one object. Our close template is shared by every object, so comparing across objects marks 91 of 94 pairs as duplicates. Measured on the replay fixture, not assumed.
  • A near-duplicate goes to review, not skip. The guard does not decide that two different sentences say the same thing; it stops the poster and hands over the comment id and score.

Verified on head 16d83500001f8e38a64a889c3dea4899b95a6607

Local, no token, no network:

$ node .github/scripts/comment-guard.mjs --replay /data/workspace/.qa-triage/replay/comments-replay.json
Replayed 26 objects; 19 flagged as duplicates; 1 missed (a duplicate stays, a comment is only written twice); 0 false positive (blocks a real comment).
[exit 0]

$ node .github/scripts/comment-guard.mjs --replay-guard /data/workspace/.qa-triage/replay/comments-replay.json
Replayed 20 objects; 14 skipped as already said, 5 sent to review, 1 would post (of which 0 are text-detectable and must not).
[exit 0]

$ node .github/scripts/label-hygiene.mjs --issues /data/tmp/issues-open.json --prs /data/tmp/prs-facts.json
#3066   agent-pr      orphan PR #3067 is closed unmerged
Scanned 28 objects; 1 orphan label(s) to strip, 0 still describe live work.
Not touched: enhancement×13 area:automation×11 needs-human×9 hold×7 area:security×3 area:docs×2 area:social×2 risk:T2×2 agent-task×1 bug×1 risk:T3×1
[exit 1]

$ node --test .github/scripts/*.test.mjs
ℹ tests 17   ℹ pass 17   ℹ fail 0

The 19: this is every comment pair QA measured. 14 are the same text with and without our marker, so they are skip; 5 were reworded by hand and are review. The would post one is #2292, two different templates for one event — similarity 0.144, the documented limit of a text check, reported as a miss instead of hidden. Misses are tolerated on purpose: a miss leaves a duplicate in place, a false positive blocks a real comment, so the --replay exit is driven by false positives only.

[exit 1] from label-hygiene is the intended signal: one orphan, so a scheduled dry run goes red on drift.

Not in this slice

  • No workflow change. tests.yml does not run these two test files yet; that is the separate CI slice (Port the risk tier engine to the 0.3.0 layout (report only) #3394 lineage), so a green CI run on this PR does not mean these 17 tests ran in CI. The numbers above are local.
  • No label removal, no comment deletion. Stripping the 19 duplicates already on GitHub needs a token and is not part of this.

Birfy added 18 commits November 26, 2026 13:08
…dgeable label red

announces_pr / work_live are not fields any issue payload carries, so on the raw
/issues?state=open snapshot the old version printed "0 orphan label(s)" and exited 0
while #3066 was an orphan. Both are gone: a label is now decided from the Branch: line
our pickup writes, plus the two listings the caller passes in (--prs, whose /pulls shape
carries head.ref, and --refs from git ls-remote).

Three verdicts, and exit 1 = orphan, 2 = cannot be judged, 0 = clean. An unknown is
never 0, so a scheduled run that lacks a fact goes red instead of reporting a clean board.

Related #3393
…-gone verdict

The two fixture tests read payloads outside the repo, so on CI they print as skipped and
their assertions could be edited without anyone noticing. Both now hash the file first
(open.json b93ccd34..., comments-replay.json 5eed6b7d...) and say in their names that CI
skips them.

New cases: the Branch: line is the only PR pointer #3066 has, so a claimed branch that is
gone decides agent-pr as an orphan from --refs alone; a payload with no listings at all
still yields unknown, never a clean board. The CLI exit codes 1 / 2 / 0 are measured by
spawning the script, not by reading its source.

Related #3393
…rgins written down

0.85 was too tight for the data we have: the weakest real duplicate in the replay fixture is
0.861 (B class, the same event twice with one extra number), so it sat 0.011 from the line -
deleting three sentences from a real duplicate dropped it to 0.771 and the guard stayed quiet.
The highest pair that is genuinely different is 0.233, so 0.80 still cannot reach it.

Same 19 duplicates found, same 0 false positives, same 1 documented miss.

Related #3393
… name that CI skips it

The fixture lives outside the repo, so on CI this test printed as skipped and anyone could
edit its assertions without a red run. It now hashes comments-replay.json first
(5eed6b7d...) and the name says "QA fixture only, skipped on CI", so the skip is visible in
the log instead of hidden behind a passing line.

Related #3393
…e case cannot pass for the wrong reason

Running the suite from anywhere but the repo root made node exit 1 on every spawn (module not
found), which is exactly what the closed-object case expects: it passed without running the
script. The path now comes from import.meta.url, and the suite gives 18/18 from both the repo
root and .github/scripts.

Related #3393
…t 3, not exit 1

Two ways this could have stripped a live label. A --refs/--prs listing that came back empty
read as "the branch is gone", which is exactly what a flaked git ls-remote returns; only a
non-empty list may now decide anything, and an empty one is unjudged (exit 2). And a mistyped
path or unparsed payload used to throw (stack, exit 1), the same code as "orphan found", so
anyone wiring `exit 1 -> strip` would have acted on their own typo; input errors now exit 3.

Also: --refs takes git ls-remote output directly (`<sha>\trefs/heads/<name>`), which is the
normal input, not a hand-made JSON array.

Exit codes: 0 clean / 1 orphan found / 2 unjudged / 3 input error.

Related #3393
…d as orphan

The bug this pins: with refs=[] the run called a branch that still exists "gone" and exited 1,
so a flaked git ls-remote plus `exit 1 -> strip` would have stripped a live label. Also covers
the raw ls-remote input form, and the fixture path behind FLYBOT_QA_FIXTURES so the QA payload
is not a hardcoded absolute path.

Related #3393
Same trap as label-hygiene: `--replay` on a path that is not there threw a stack and exited 1,
which a caller reading `exit 1 -> do not post` cannot tell apart from "this comment really is a
duplicate". Input errors are now 3 with one line on stderr.

Exit codes: 0 ok / 1 a real comment would be blocked / 3 input error.

Related #3393
The replay fixture was a hardcoded absolute path, so on any other machine the two replay tests
silently skipped. Now they skip only when the directory really is absent, and the skip text says
so; the sha256 still guards the numbers in the assertions.

Related #3393
…efs)

- an empty or absent listing, against a branch that really exists, must exit 2 (unjudged) and
  never 1: a flaked git ls-remote is exactly `[]`, and 1 reads as "strip".
- `git ls-remote` output must be accepted directly and produce byte-identical output to the
  JSON array; a tags-only listing says nothing about branches, so 2.
- a missing file, unparsed JSON and a bare invocation are 3 with one line of stderr, not a stack
  and not 1. Same for `comment-guard --replay` on a missing path.

Related #3393
… a lost copy-paste)

502e0bb/2460421c were fine, but the comment-guard.mjs I pushed imported norm/similar from
comment-text.mjs, which exports none of those names: the file on the branch was a mangled merge
of two drafts. This restores the version that was measured, keeping the exit-3 wrapper.

Related #3393
Same copy-paste problem as the previous commit: this file was a draft for a design that does not
exist. It now tests the four classes against the real exports, and reads the QA fixture through
FLYBOT_QA_FIXTURES so a different machine skips instead of reading a hardcoded path.

Related #3393
The extra empty-list case I added here duplicates the one in cli-exit-codes.test.mjs, which
drives it through the CLI instead of the API. One file each, as measured: 21 tests.

Related #3393
@Birfy

Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

退出码表(head 96de428a55d6bf7d3180ffbac203f28d67fec27d)

接线方读到的唯一契约。写操作一律只认 1 的语义,因此输入错误必须与「发现孤儿」分开。

code label-hygiene.mjs comment-guard.mjs
0 clean:没有要摘的标签,每条判定都有事实支撑 ok:没有命中,可以发/没有阻断
1 orphan found:有标签对应的分支/PR 已不存在 一条真实的新评论会被判重挡住(--replay 下为 0 false positive 的对立面)
2 unjudged:--refs/--prs 没给或给了空列表,有标签无法判定 —
3 input error:文件不存在、JSON 解析失败、参数不合法(一行 stderr,无栈) input error:同上;--replay/--replay-guard 文件缺失也是 3

comment-guard --comments ... --check-only 另有细分:0 可发 / 3 已说过(skip)/ 4 需人看(review)。

两条硬约束(不是风格问题)

  1. 空列表不是事实。 git ls-remote 抖动就会返回空输出,读成「分支已删」再摘标签就是误摘活标签。只有非空列表可以下 orphan 判定。
  2. 1 只表示「找到了」,不表示「出错了」。把路径写错、JSON 坏掉归到 1,会让 exit 1 → 摘标签/不发评论 这类接线在拼错路径时动手。

--refs 接受两种输入

git ls-remote --heads origin > refs.txt      # <sha>\trefs/heads/<name>,这是常态输入
node .github/scripts/label-hygiene.mjs --issues open.json --refs refs.txt
node .github/scripts/label-hygiene.mjs --issues open.json --refs refs.json   # JSON 数组或 {"refs":[...]}

两者输出逐字节相同(本机 53 条 heads 实测)。

注意:这个 PR 的 CI 不会跑这 21 个测试

tests.yml 未接入 .github/scripts/*.test.mjs(切片 ① 卡在 #3394)。因此本 PR 上任何绿色都与这些测试无关,验证只能靠下面的命令在本地重现:

node --test .github/scripts/*.test.mjs          # 21 pass / 0 fail(未跳过:QA 夹具在本机)
git ls-remote --heads origin > /tmp/refs.txt
node .github/scripts/label-hygiene.mjs --issues <saved /issues?state=open>                    # exit 2, unjudged
node .github/scripts/label-hygiene.mjs --issues <...> --refs /tmp/refs.txt                    # exit 1, 1 orphan (#3066)
node .github/scripts/label-hygiene.mjs --issues <...> --refs /dev/stdin <<< '[]'              # exit 2, 绝不出 orphan
node .github/scripts/label-hygiene.mjs --issues /nope.json                                    # exit 3, 一行 stderr

夹具路径默认 /data/workspace/.qa-triage,可用 FLYBOT_QA_FIXTURES=/path 覆盖;夹具不在的机器上这两个回放测试打印为 skipped,而不是静默通过。

Related #3393

An agent-pr row that already got --refs was still told "pass --prs or --refs":
a caller wiring this up re-passes the flag forever and the run never settles.
Each row now names just the absent fact, and when both were given it says the
payload itself is what has to name the PR. The empty-PR-list case keeps the
existing "no PR list was given" wording (a flaked ls-remote must not read as
an orphan). No verdict changes: a live branch still cannot prove an open PR
carries the label, so that row stays unknown / exit 2 by design.
Three spawn-level cases on one fixture: --refs given (the row must not say
--refs and the summary must ask for --prs), --prs given (the mirror), and both
given (no flag at all, because then the payload is what is missing). The
agent-pr row stays unknown/exit 2 in all three: a live branch cannot prove an
open PR still carries the label.
@Birfy

Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Exit codes, one table per subcommand

Supersedes #issuecomment-7184438697 — that table put --replay and --replay-guard under one "exit 1", and the two mean opposite things. Bound to head 6641a833018c52c8834547372b32053e6eeb0e0e.

label-hygiene.mjs (report only, never writes)

exit meaning what wiring should do
0 clean: every in-scope label still describes something nothing
1 at least one agent-working / agent-pr is an orphan strip those labels, then re-run to confirm 0
2 at least one label could not be judged: the fact was not supplied, or the list given was empty strip nothing; supply the missing --prs / --refs
3 input error: missing file, bad JSON, no arguments stop and fix the input

--refs takes git ls-remote output as it comes out (<sha>\trefs/heads/<name>) or a JSON array; both give byte-identical output. An empty list is not a fact — a flaked ls-remote gives exactly that, so it stays unjudged, never orphan.

comment-guard.mjs --replay <fixture> — did the matcher call real duplicates duplicates?

exit meaning
0 no false positive: every comment it would skip really is a duplicate
1 it would skip a real comment (false positive) — the failure that matters in this mode
3 input error

comment-guard.mjs --replay-guard <fixture> — would the guard have caught the rerun?

exit meaning
0 nothing postable slipped through
1 an A/B-class duplicate (text-detectable by construction) would still be posted — a miss
3 input error

C class (#2292, similarity 0.144) may still post: two different templates for one event are past the limit of a text check, and the run reports it as a miss instead of hiding it. That is why only A/B count toward this exit.

comment-guard.mjs --comments … --check-only — on a live object

exit meaning
0 would post
3 skip: the same marker is already there, or the same text is already on the object
4 near-duplicate: a human decides

In this mode 3 has two sources — "already said" and "input error" — and they are told apart by whether stderr carries comment-guard: input error: …. Treat a 3 without that line as "already said".

comment-guard.mjs --comments … (no --check-only)

exit meaning
0 always: it prints the verdict (skip / review / post) and never fails the run
3 input error

Do not gate on this mode's exit status; gate on --check-only, which maps the verdict to 0/3/4.

One design note that wiring must not "fix"

An agent-pr issue that claims a live branch but names no PR stays unknown / exit 2. A live branch cannot prove an open PR still carries the label, so the honest answer is "I do not know" — and the wiring table reads 2 as do not strip. agent-working decides on the branch, agent-pr decides on the PR; they are different facts on purpose.

Reproduce locally, no token:

node --test '.github/scripts/*.test.mjs'
node .github/scripts/comment-guard.mjs --replay <fixture>/comments-replay.json
node .github/scripts/comment-guard.mjs --replay-guard <fixture>/comments-replay.json
node .github/scripts/label-hygiene.mjs --issues <fixture>/raw/open.json
node .github/scripts/label-hygiene.mjs --issues <fixture>/raw/open.json --refs <ls-remote --heads output>

CI on this PR does not run these tests: tests.yml still does not cover .github/scripts/*.test.mjs (that is the separate slice behind #3394), so a green run here says nothing about them.

@Birfy

Birfy commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator Author

Lead review record — head 6641a833018c52c8834547372b32053e6eeb0e0e

Binding correction: the description still says Verified on head 16d83500… with tests 17. That head is dead and those numbers are void. No one in this run has a tool to edit a pull request description (no PATCH /pulls/{n} in the available action set, no token), so this comment and #issuecomment-7184681371 are the canonical record for this head, not the body. The body needs one edit whenever a token exists: 16d83500 → 6641a833, 17 → 22, three test files, and the current output format 0 orphan(s) to strip, 0 live, 1 unjudged.

Checked by me independently on the clean tree from refs/pull/3395/head at 6641a833 (6 files, +646/−0, base 34a80ca5a1):

check result
node --test '.github/scripts/*.test.mjs' tests 22 / pass 22 / fail 0 / skipped 0
missing file, {broken, no args, --replay missing file each one line of stderr, no stack, exit 3
--refs [] / --refs ["main"] exit 2 / exit 1
agent-pr with a live branch and no named PR exit 2, reason names only --prs, the summary line no longer asks for --refs
ls-remote raw text vs JSON array diff empty, both exit 1
--replay 19 flagged / 1 missed / 0 false positive, exit 0
--replay-guard 14 skipped / 5 review / 1 would post, exit 0
7 false-positive probes failures: 0

agent-pr with a live Branch: line and no named PR staying at exit 2 is intended, not a defect: a live branch cannot show that a PR still carries the work, and exit 2 means "do not strip". Only agent-working may read a live branch as keep.

Local green, not CI green: tests.yml does not run these 22 tests, so a green run on this PR does not mean they ran in CI. Wiring them into a workflow is the next slice, and it is blocked on the risk/audit workflow slice landing (#3394 lineage) rather than on this PR.

Related #3393

@Birfy

Birfy commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

QA evidence (exact head)

Head 6641a833018c52c8834547372b32053e6eeb0e0e · branch dev/3393-write-guards · base = merge-base with main 34a80ca5
Issued 2026-11-27T05:15Z · Status: local green | CI: historical green only, on a merge ref built from an older main (see 2) — no CI claim for the tree that would land
Valid only for the 40-char head above. Evidence produced by QA Engineer; posted by FlyBot Lead because the QA bot has no write token.

1. Binding

codeload tar.gz 6641a833 -> unpack -> git add -A -f . && git write-tree
c4b71efb3da74f016507f422e4d1126a6e03b370
$ git rev-parse 6641a833^{tree}
c4b71efb3da74f016507f422e4d1126a6e03b370
$ git ls-remote origin refs/pull/3395/head
6641a833018c52c8834547372b32053e6eeb0e0e  refs/pull/3395/head

Diff vs 34a80ca5: 6 files, +646/−0, all new files:

file result
.github/scripts/comment-guard.mjs new
.github/scripts/comment-text.mjs new
.github/scripts/comment-guard.test.mjs new
.github/scripts/label-hygiene.mjs new
.github/scripts/label-hygiene.test.mjs new
.github/scripts/cli-exit-codes.test.mjs new

None of these 6 paths is touched by the 7 commits main took after 34a80ca5 (intersection with git diff --name-only 34a80ca5 60e80980 is empty), and every file is an addition, so merging cannot revert anything main holds. mergeable=true, state clean.

2. CI (readings only)

check-runs run window
16/16 success 123832840107 2026-11-26T06:38Z → 06:43Z

That run is on a merge ref whose first parent is afbfae99, six commits behind current main 60e80980 (it predates #3402 merge-gate, #3404 risk-gate and #3406 tests.yml). The .github/scripts/*.test.mjs glob is not in that tree, so the tests in this PR did not run in CI there. The landing tree has never been through CI; the main push run after the merge is its first real check.

3. Local results (this machine, not CI)

check result
node --test .github/scripts/*.test.mjs in the head tree 22 pass, 0 fail
fixtures pointed at a non-existent path 16 pass / 2 skipped / 0 fail (no /data dependency, cannot go red on a runner)
replay of the body's dedup assertion on this head 26 objects, 19 flagged, 1 missed, 0 false positives, rc=0

4. Body vs head (record layer, bytes untouched)

  • The body says "Verified on head 16d83500" with 17/17. The head is 6641a833, ten commits later, and one file was added (cli-exit-codes.test.mjs) → 22/22 now. The 17/17 and replay numbers written for 16d83500 are void for this head.

5. Labels

None. risk-gate.yml only reached main on 2026-11-27 (#3404), after this branch's opened/synchronize events, so no risk:T* label was produced for this head and none is being back-filled by hand. Tier status for the merge record: not produced.

@Birfy

Birfy commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator Author

Lead review (exact head)

Reviewed head 6641a833018c52c8834547372b32053e6eeb0e0e · base merge-base 34a80ca5 · main at review time 60e80980
Verdict: merge. One reviewable slice, and it is the second of the three machine-forced things the goal asks for.

Gates

gate result
one reviewable slice yes — write-path idempotency (comment-guard.mjs, comment-text.mjs) and orphan-label hygiene (label-hygiene.mjs), plus their tests. No workflow change, no required check renamed, no tests.yml edit.
bound to an exact head yes, tree c4b71efb recomputed from downloaded bytes equals 6641a833^{tree}
local vs CI reported separately yes — QA evidence comment above keeps them apart, and states plainly that the CI green is on a merge ref six commits behind main
forbidden close keywords none in the diff, the commit messages or the body (`Fixes
risk tier not produced, and not back-filled by hand. risk-gate.yml reached main on 2026-11-27 (#3404), after this branch's opened/synchronize events. Per the 2026-11-26 decision the tier is advisory anyway; the merge decision and its reason are recorded here instead.
English-only rule satisfied except for fixture strings in comment-guard.test.mjs — see the ruling below

What I checked myself

  • refs/pull/3395/head = 6641a833… (git protocol, read 2026-11-27T05:20Z), tree c4b71efb3da74f016507f422e4d1126a6e03b370, 6 files +646/−0 against 34a80ca5.
  • Intersection of those 6 paths with git diff --name-only 34a80ca5 60e80980 is empty; every path is new. Merging cannot revert anything main has merged since.
  • The two new scripts are read-only helpers: no fetch, no writeFile, no spawn, so nothing in this slice can write to the repository on its own.

Ruling on the Chinese fixture strings (comment-guard.test.mjs, 18 lines / 231 CJK chars)

Keep as-is. These lines are verbatim bodies of real historical comments being replayed to prove deduplication (26 objects / 19 flagged / 1 missed / 0 false positives). They are input data, not prose we author, and translating them would make the replay assert on text that never existed in the repository. The English rule binds the artefacts we write — file prose, code comments, commit messages, PR/issue text and comments — and this file's own prose is English. Same ruling covers the two fixtures/silent-revert/*.json payloads in #3397.

Record corrections (bytes untouched)

The body's "Verified on head 16d83500 / 17/17" is void for this head: ten commits later there are 22 tests. Anyone quoting this PR's numbers must quote the QA evidence above, not the body.

Merge reason

Ships the second of the three machine-forced guarantees (idempotent comments, orphan labels) as six read-only script files that never run in CI unless a later slice wires them — so the risk of landing it is limited to the bytes themselves, and its tests enter CI through the tests.yml glob that #3406 merged. The CI green on this PR predates that glob, so the first real CI pass for these 22 tests is the main push run right after this merge; that is recorded, not glossed over.

Expected head for the queue: 6641a833018c52c8834547372b32053e6eeb0e0e.

@Birfy
Birfy added this pull request to the merge queue Nov 27, 2026
Merged via the queue into main with commit 68e2322 Nov 27, 2026
16 checks passed
@Birfy
Birfy deleted the dev/3393-write-guards branch November 27, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant